<?php
namespace App\Http\Controllers\Company;

use App\Exceptions\ApiException;
use App\Http\Requests\BasicRequest;
use App\Mail\Basic;
use App\Models\ClientUserRelationship;
use App\Models\HouseDistrict;
use App\Models\ManageMessage;
use App\Models\ManageMessageSend;
use App\Models\ServiceTeam;
use App\Models\ServiceTeamDetail;

class ManageMessageController extends CommonController {

    /**
     * @param BasicRequest $request
     * @return array
     * 内部沟通列表
     */
    public function list(BasicRequest $request){
        $return = ManageMessage::leftJoin('manage_message_send','manage_message_send.id','=','manage_message.send_id')
            ->leftJoin('client_user','client_user.id','=','manage_message.send_uid')
            ->select('manage_message.id','manage_message.uid','manage_message.send_uid','manage_message.is_view','manage_message_send.content','client_user.realname','client_user.avatar')
            ->where(['manage_message.uid'=>$this->uid,'manage_message.is_del'=>0])
            ->pages();
        ManageMessage::where(['uid'=>$this->uid,'is_view'=>0])->update(['is_view'=>1]);
        return json_success('OK',$return);
    }

    /**
     * @param BasicRequest $request
     * @return array
     * 内部沟通详情
     */
    public function detail(BasicRequest $request){
        $id = $request->input('id');
        $detail = ManageMessage::leftJoin('manage_message_send','manage_message_send.id','=','manage_message.send_id')
            ->leftJoin('client_user','client_user.id','=','manage_message.send_uid')
            ->select('manage_message.uid','manage_message.send_uid','manage_message.is_view','manage_message_send.content','client_user.realname','client_user.avatar')
            ->where(['manage_message.uid'=>$this->uid,'manage_messageis_del'=>0,'manage_message.id'=>$id])
            ->first();
        return json_success('OK',$detail);

    }

    /**
     * @param BasicRequest $request
     * @return array
     * 发送内部通知
     */
    public function send(BasicRequest $request){
        $ids = $request->input('ids');
        $content = $request->input('content');
        $return = ManageMessageSend::send($this->uid,$content,$ids,$this->ship->village_id);
        return json_success('OK',$return);
    }
}